package edu.ncst.baseweb.service.impl.webmanage;

import cn.wintersun.basecommon.common.Result;
import edu.ncst.baseweb.dao.web.LabDownload;
import edu.ncst.baseweb.mapper.LabDownloadMapper;
import edu.ncst.baseweb.service.intrer.webmanage.LabDownloadManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class LabDownloadManageImpl implements LabDownloadManageService {

    @Autowired private LabDownloadMapper LabDownloadMapper;


    @Override
    public Result<IPage<LabDownload>> list(LabDownload LabDownload) {
        QueryWrapper<LabDownload> queryWrapper = Wrappers.query();


        Page<LabDownload> LabDownloadPage = new Page<>(LabDownload.getCurrent(), LabDownload.getPageSize());
        IPage<LabDownload> LabDownloadIPage = LabDownloadMapper.selectPage(LabDownloadPage, queryWrapper);

        return Result.ok(LabDownloadIPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> save(LabDownload LabDownload) {
        if (LabDownload.getId() != null) {
            //修改
            LabDownloadMapper.updateById(LabDownload);

        } else {
            //新增
            LabDownloadMapper.insert(LabDownload);
        }

        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> delete(String LabDownloadId) throws Exception {

        if (StringUtils.isBlank(LabDownloadId)) {
            throw new Exception("被删除的id为空");
        }

        List<String> ids = Arrays.stream(LabDownloadId.split(",")).collect(Collectors.toList());

        // 删除  id 在 ids 集合中的记录
        LabDownloadMapper.deleteBatchIds(ids);

        return Result.failure(200, "删除成功！");
    }
    @Override
    public String uploadFile(MultipartFile file, Integer id){
        try {

            String fileExtension = getFileExtension(Objects.requireNonNull(file.getOriginalFilename()));

            // 定义不同类型的文件扩展名
            List<String> pdfExtensions = Collections.singletonList("pdf");

          if (pdfExtensions.contains(fileExtension)){
                String basePath = System.getProperty("user.dir");
                String filePath = basePath + "/files/files/" + file.getOriginalFilename();
                file.transferTo(new File(filePath));
                LabDownloadMapper.updateFileById(id,file.getOriginalFilename(),filePath);
                return "文档上传成功";
            }
            return "上传成功";
        } catch (Exception e) {
            return "上传失败: " + e.getMessage();
        }
    }
    //获取文件扩展名
    private String getFileExtension(String fileName) {
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex >= 0 && dotIndex < fileName.length() - 1) {
            return fileName.substring(dotIndex + 1).toLowerCase();
        }
        return "";
    }
}
